package com.sinoglobal.sinostore.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;
import com.orhanobut.logger.Logger;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.SinoStore;
import com.sinoglobal.sinostore.activity.GoodsDetailActivity;
import com.sinoglobal.sinostore.activity.GoodsListActivity;
import com.sinoglobal.sinostore.activity.SearchListActivity;
import com.sinoglobal.sinostore.activity.SecondKillActivity;
import com.sinoglobal.sinostore.adapter.ActivityShopCityAdapter;
import com.sinoglobal.sinostore.adapter.EveryOneIsBuyAdapter;
import com.sinoglobal.sinostore.adapter.ImagePagerAdapter;
import com.sinoglobal.sinostore.adapter.TheZoneAdapter;
import com.sinoglobal.sinostore.bean.ActiveListVo;
import com.sinoglobal.sinostore.bean.ActiveVo;
import com.sinoglobal.sinostore.bean.ImageVo;
import com.sinoglobal.sinostore.bean.ShopCityFirstVo;
import com.sinoglobal.sinostore.bean.ShopWareList;
import com.sinoglobal.sinostore.bean.ShopWareTypeVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
import com.sinoglobal.sinostore.utils.BitmapHelp;
import com.sinoglobal.sinostore.utils.DateUtils;
import com.sinoglobal.sinostore.widget.AutoScrollViewPager;
import com.sinoglobal.sinostore.widget.MyGridView;

import java.util.ArrayList;

import andbase.util.AbAppUtil;
import andbase.view.pullview.AbPullToRefreshView;
import andbase.view.pullview.AbPullToRefreshView.OnHeaderRefreshListener;

/**
 * @author LK
 * @title: FirstFragment.java
 * @package com.chiliring.sinostore.fragment
 * @description: TODO(商城首页)
 * @date 2015-1-13 上午9:27:03
 */
public class FirstFragment extends Fragment implements OnClickListener, OnHeaderRefreshListener {
    /**
     * 未开始
     **/
    private static final int NOTATTHE = 1;
    /**
     * 抢购中
     **/
    private static final int SNAPINUP = 2;
    /**
     * 结束
     **/
    private static final int ENDOFTHE = 3;

    /**
     * 搜索商品
     **/
    private EditText etSearch;

    /**
     * 轮播图
     **/
    private AutoScrollViewPager bannerViewPager;

    private ImagePagerAdapter imagePagerAdapter;

    /**
     * 轮播图上的小点
     **/
    private LinearLayout llFocusaIndicatorContainer;

    /**
     * 商品类目
     **/
    private MyGridView categoryGridView;

    /**
     * 秒杀商品
     **/
    private MyGridView gvSecondKill;

    /**
     * 专区列表
     **/
    private MyGridView gvTheZone;

    /**
     * 大家都在买
     **/
    private MyGridView gvEveryoneIsBuying;

    /**
     * 限时秒杀查看更多
     **/
    private TextView tvLoadMore;

    /**
     * 下拉刷新
     **/
    private AbPullToRefreshView shopFirstToRefreshView;

    private ArrayList<ImageVo> imgList;//轮播图集合
    private int preSelImgIndex = 0; // 存储上一个选择项的Index

    private ActivityShopCityAdapter adapter;

    /**
     * 专区列表adapter
     **/
    private TheZoneAdapter theZoneAdapter;

    /**
     * 大家都在买
     **/
    private EveryOneIsBuyAdapter everyOneIsBuyAdapter;

    /**
     * 倒计时
     **/
    private LinearLayout llCountdown;

    /**
     * 小时
     **/
    private TextView tvHour;
    /**
     * 分钟
     **/
    private TextView tvMinutes;
    /**
     * 秒
     **/
    private TextView tvSeconds;
    /**
     * 提示
     **/
    private TextView tvKilling;
    /**
     * 加载数据
     **/
    private LinearLayout reLoadData;

    private Handler mHandler;

    private Runnable runnable;

    /**
     * 首页实体类
     **/
    private ShopCityFirstVo entity;

    /**
     * 倒计时时间
     **/
    private long customTime;

    View rlSpecialOffer;//专区特卖
    ImageView ivSpecialOffer1, ivSpecialOffer2, ivSpecialOffer3;//专区特卖的图片
    LinearLayout llRecommendHead;//大家都在买的提示信息

//	MiaoShaVo miaoShaVo;
    /**
     * 计时
     **/
    int timing = 0;
    private View layoutSearch;
    private View layoutActiveZone;//活动区
    private View secondKillZone;//秒杀区
    private ImageView ivSecondKill;
    private ProgressDialog progressDialog;
    private String secondKillId;//秒杀id
    private View bannerLayout;//轮播图展示区
    private View goodsDetailsview;
    /**
     * 子活动Id
     **/
    private String activityId;

    private ImageButton titleBtnLeft;
    /**
     * 子活动
     **/
    private ActiveVo activeVo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("加载中...");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        goodsDetailsview = inflater.inflate(R.layout.shop_fragment_first_layout, null);
        initView(goodsDetailsview);
        initData(true);
        return goodsDetailsview;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    /**
     * @author LK
     * @methods initView
     * @description 描述信息 初始化界面信息
     * @date 2015-1-13 上午9:33:30 参数说明
     */
    private void initView(View view) {
        shopFirstToRefreshView = (AbPullToRefreshView) view.findViewById(R.id.shop_first_layout);
        shopFirstToRefreshView.setVisibility(View.INVISIBLE);

        bannerLayout = view.findViewById(R.id.bannerLayout);
        layoutActiveZone = view.findViewById(R.id.llActiveZone);
        ivSecondKill = (ImageView) view.findViewById(R.id.ivSecondKill);
        secondKillZone = view.findViewById(R.id.secondKillZone);
        etSearch = (EditText) view.findViewById(R.id.et_search_goods);
        titleBtnLeft = (ImageButton) view.findViewById(R.id.title_btn_left);
        reLoadData = (LinearLayout) view.findViewById(R.id.reLoadView);


        layoutSearch = view.findViewById(R.id.layout_search);
        layoutSearch.setOnClickListener(this);
        etSearch.setFocusable(false);
        etSearch.setOnClickListener(this);
        titleBtnLeft.setOnClickListener(this);
        reLoadData.setOnClickListener(this);
        titleBtnLeft.setVisibility(View.VISIBLE);
        bannerViewPager = (AutoScrollViewPager) view.findViewById(R.id.banner_viewpager);
        llFocusaIndicatorContainer = (LinearLayout) view.findViewById(R.id.ll_focus_indicator_container);
        categoryGridView = (MyGridView) view.findViewById(R.id.gv_goods_type);
        gvSecondKill = (MyGridView) view.findViewById(R.id.gv_second_kill);
        gvSecondKill.setEmptyView(ivSecondKill);
        gvTheZone = (MyGridView) view.findViewById(R.id.gv_thezone);
        gvEveryoneIsBuying = (MyGridView) view.findViewById(R.id.gv_everyone_is_buying);
        tvLoadMore = (TextView) view.findViewById(R.id.tv_loadmore);
        llCountdown = (LinearLayout) view.findViewById(R.id.ll_countdown);
        tvHour = (TextView) view.findViewById(R.id.tv_hour);
        tvMinutes = (TextView) view.findViewById(R.id.tv_minutes);
        tvSeconds = (TextView) view.findViewById(R.id.tv_second);
        rlSpecialOffer = view.findViewById(R.id.rlSpecialOffer);
        ivSpecialOffer1 = (ImageView) view.findViewById(R.id.content_img1);
        ivSpecialOffer2 = (ImageView) view.findViewById(R.id.content_img2);
        ivSpecialOffer3 = (ImageView) view.findViewById(R.id.content_img3);
        llRecommendHead = (LinearLayout) view.findViewById(R.id.llRecommendHead);
        tvKilling = (TextView) view.findViewById(R.id.tv_killing);
        tvLoadMore.setOnClickListener(this);

        shopFirstToRefreshView.setOnHeaderRefreshListener(this);
        shopFirstToRefreshView.setPullRefreshEnable(true);
        shopFirstToRefreshView.setLoadMoreEnable(false);

        LinearLayout.LayoutParams layoutParams = (LayoutParams) bannerViewPager.getLayoutParams();
        layoutParams.height = (int) (SinoStore.WIDTHPIXELS / (640 / 354.0));
        bannerViewPager.setLayoutParams(layoutParams);
        bannerViewPager.setInterval(3000);
        bannerViewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (isAdded()) {
                    if (imgList.size() > 1) {
                        // 修改上一次选中项的背景
                        position = position % imgList.size();
                        ImageView preSelImg = (ImageView) llFocusaIndicatorContainer.findViewById(preSelImgIndex);
                        preSelImg.setImageDrawable(getResources().getDrawable(R.drawable.shop_banner_img_selected));
                        // 修改当前选中项的背景
                        ImageView curSelImg = (ImageView) llFocusaIndicatorContainer.findViewById(position);
                        curSelImg.setImageDrawable(getResources().getDrawable(R.drawable.shop_banner_img_default));
                        preSelImgIndex = position;
                    }
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    /**
     * @author LK
     * @methods initData
     * @description 描述信息  初始化数据
     * @date 2015-1-13 上午9:33:59 参数说明
     */
    private void initData(final boolean isShowDialog) {
        if (!isAdded()) {
            return;
        }
        if (AbAppUtil.isNetworkAvailable(getActivity())) {
            reLoadData.setVisibility(View.GONE);
            HttpUtils httpUtils = new HttpUtils();
            RequestParams params = new RequestParams();
            params.addQueryStringParameter("por", "100");
            params.addQueryStringParameter("proIden", Constants.STOREFLAG);
            ApiDebugUtil.debug(params);
            httpUtils.send(HttpMethod.POST, Constants.STORE_URL, params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onStart() {
                            super.onStart();
                            Logger.i(" 请求链接====" + this.getRequestUrl());
                            if (isShowDialog) {
                                try {
                                    progressDialog.show();
                                } catch (Exception e) {
                                }
                            }
                        }

                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            shopFirstToRefreshView.setVisibility(View.GONE);
                            reLoadData.setVisibility(View.VISIBLE);
                            try {
                                progressDialog.dismiss();
                            } catch (Exception e) {
                            }
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            shopFirstToRefreshView.onHeaderRefreshFinish();
                            if (isShowDialog) {
                                loadData();
                            }
                            String result = responseInfo.result;
                            Logger.e("result==" + result);
                            if (!TextUtils.isEmpty(result)) {
                                try {
                                    entity = JSON.parseObject(result, ShopCityFirstVo.class);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    shopFirstToRefreshView.setVisibility(View.GONE);
                                    reLoadData.setVisibility(View.VISIBLE);
                                    layoutActiveZone.setVisibility(View.GONE);
                                    return;
                                }
                                if (Constants.STORE_SUCCESS_CODE.equals(entity.getCode())) {
                                    if (isAdded()) {
//										shopFirstToRefreshView.setVisibility(View.VISIBLE);
                                        goodsDetailsview.setVisibility(View.VISIBLE);
                                        Constants.PIC_HOST = entity.getHost();
                                        if (null == entity.getLunbo()
                                                || entity.getLunbo().size() == 0) {
                                            bannerLayout.setVisibility(View.GONE);
                                        } else {
                                            // 配置轮播图
                                            initCategoryViewPager(entity.getLunbo());
                                        }
                                        // 配置类目信息
                                        ShopWareTypeVo shopWareTypeVo = new ShopWareTypeVo();
                                        shopWareTypeVo.setName(getResources().getString(R.string.shop_alltype));
                                        shopWareTypeVo.setId(String.valueOf(0));
                                        entity.getFenlei().add(0, shopWareTypeVo);
                                        if (entity.getFenlei() != null) {
                                            initGridView(entity.getFenlei());
                                        }
                                        ArrayList<ActiveListVo> activeListVos = entity.getActivity();
                                        if (activeListVos == null
                                                || activeListVos.isEmpty()) {
                                            layoutActiveZone.setVisibility(View.GONE);
                                        } else {
                                            layoutActiveZone.setVisibility(View.VISIBLE);
                                            ActiveListVo miaoShaVo = null, specialOfferVos = null, recommendActiveVos = null;
                                            for (int i = 0, lenth = activeListVos.size(); i < lenth; i++) {
                                                if ("1".equals(activeListVos.get(i).getType())) {// 秒杀
                                                    miaoShaVo = activeListVos.get(i);
                                                }
                                                if ("2".equals(activeListVos.get(i).getType())) {// 特卖
                                                    specialOfferVos = activeListVos.get(i);
                                                }
                                                if ("3".equals(activeListVos.get(i).getType())) {// 推荐
                                                    recommendActiveVos = activeListVos.get(i);
                                                }
                                            }
                                            // 配置秒杀信息
                                            if (miaoShaVo != null) {
                                                initSecondKill(miaoShaVo, entity.getHost());
                                            }
                                            // 配置特卖信息
                                            if (specialOfferVos != null) {
                                                initSpecialOffer(specialOfferVos, entity.getHost());
                                            } else {
                                                rlSpecialOffer.setVisibility(View.GONE);
                                            }
                                            // 配置推荐信息
                                            if (recommendActiveVos != null) {
                                                initTheZone(recommendActiveVos);
                                            }
                                            goodsDetailsview.scrollTo(0, 0);
                                        }
                                        shopFirstToRefreshView.setVisibility(View.VISIBLE);
                                    }
                                } else {
                                    Toast.makeText(getActivity(), entity.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    });
        } else {
            shopFirstToRefreshView.setVisibility(View.GONE);
            reLoadData.setVisibility(View.VISIBLE);
            try {
                progressDialog.dismiss();
            } catch (Exception e) {
            }
            Toast.makeText(getActivity(), "网络好像不给力哦,请稍候再试", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * @author LK
     * @methods loadData
     * @description 加载推荐商品信息
     * @date 2015-2-4 下午3:27:24 参数说明
     */
    private void loadData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("por", "109");
        params.addQueryStringParameter("proIden", Constants.STOREFLAG);
        params.addQueryStringParameter("type", "2");
        httpUtils.send(HttpMethod.POST, Constants.STORE_URL, params, new RequestCallBack<String>() {

            @Override
            public void onStart() {
                super.onStart();
                LogUtils.i(" 请求链接====" + this.getRequestUrl());
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                try {
                    progressDialog.dismiss();
                } catch (Exception e) {
                }
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    progressDialog.dismiss();
                } catch (Exception e) {
                }
                String result = responseInfo.result;
                LogUtils.e("result==" + result);
                if (!TextUtils.isEmpty(result)) {
                    ShopCityFirstVo cityFirstVo;
                    try {
                        cityFirstVo = JSON.parseObject(result, ShopCityFirstVo.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getActivity(), "系统异常，请重试", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Constants.STORE_SUCCESS_CODE.equals(cityFirstVo.getCode())) {
                        Constants.PIC_HOST = cityFirstVo.getHost();
                        if (null != cityFirstVo.getList() && cityFirstVo.getList().size() > 0) {
                            llRecommendHead.setVisibility(View.VISIBLE);
                            initEveryoneIsBuying(cityFirstVo.getList());
                        } else {
                            llRecommendHead.setVisibility(View.GONE);
                        }
                    } else if (Constants.NODATA_CODE.equals(cityFirstVo.getCode())) {
                        llRecommendHead.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    /**
     * @author zrh
     * @methods initSpecialOffer
     * @description 配置 专区特卖
     * @date 2015年1月21日 下午1:22:40 参数说明
     */
    private void initSpecialOffer(ActiveListVo specialOfferVos, String imgHostUrl) {
        BitmapUtils bitmapUtils = BitmapHelp.getBitmapUtils(getActivity());
        if (specialOfferVos.getList() == null) {
            rlSpecialOffer.setVisibility(View.GONE);
            return;
        }
        switch (specialOfferVos.getList().size()) {
            case 0:
                rlSpecialOffer.setVisibility(View.GONE);
                break;
            case 1:
                bitmapUtils.display(ivSpecialOffer1, imgHostUrl + specialOfferVos.getList().get(0).getImg());
                break;
            case 2:
                bitmapUtils.display(ivSpecialOffer1, imgHostUrl + specialOfferVos.getList().get(0).getImg());
                bitmapUtils.display(ivSpecialOffer2, imgHostUrl + specialOfferVos.getList().get(1).getImg());
                break;
            case 3:
                bitmapUtils.display(ivSpecialOffer1, imgHostUrl + specialOfferVos.getList().get(0).getImg());
                bitmapUtils.display(ivSpecialOffer2, imgHostUrl + specialOfferVos.getList().get(1).getImg());
                bitmapUtils.display(ivSpecialOffer3, imgHostUrl + specialOfferVos.getList().get(2).getImg());
                break;

            default:
                break;
        }
    }

    /**
     * @author LK
     * @methods initSecondKill
     * @description 描述信息 初始化秒杀商品信息
     * @date 2015-1-13 上午11:47:33 参数说明
     */
    private void initSecondKill(final ActiveListVo miaoShaVo, String imgHost) {
        if (miaoShaVo.getList() == null || miaoShaVo.getList().isEmpty()) {
            secondKillZone.setVisibility(View.GONE);
            return;
        } else {
            secondKillZone.setVisibility(View.VISIBLE);
        }
        BitmapUtils bitmapUtils = BitmapHelp.getBitmapUtils(getActivity());
        LinearLayout.LayoutParams layoutParams = (LayoutParams) ivSecondKill.getLayoutParams();
        layoutParams.height = (int) (SinoStore.WIDTHPIXELS / (640 / 264.0));
        ivSecondKill.setLayoutParams(layoutParams);
        bitmapUtils.display(ivSecondKill, imgHost + miaoShaVo.getList().get(0).getImg());
        ivSecondKill.setOnClickListener(this);
        secondKillId = miaoShaVo.getId();
        try {
            activeVo = miaoShaVo.getList().get(0);
            activityId = activeVo.getId();
            dealActivityState();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author LK
     * @methods dealActivityState
     * @description 处理订单状态
     * @date 2015-2-4 下午2:21:54 参数说明
     */
    private void dealActivityState() {
        if (null == activeVo) {
            secondKillZone.setVisibility(View.GONE);
            return;
        }
        switch (activeVo.getStatus()) {
            //秒杀未开始
            case NOTATTHE:
                if (TextUtils.isEmpty(activeVo.getStart_time()) || TextUtils.isEmpty(entity.getServer_time())) {
                    return;
                }
                Long startTime = Long.parseLong(activeVo.getStart_time());
                Long serverTime1 = Long.parseLong(entity.getServer_time());
                Long countdown = startTime - serverTime1;
                if (countdown > 86400) {
                    llCountdown.setVisibility(View.GONE);
                    tvKilling.setText(String.format("%d开始", DateUtils.parseTimeStampToDateString(startTime)));
                    tvKilling.setVisibility(View.VISIBLE);
                } else {
                    tvKilling.setVisibility(View.GONE);
                    if (countdown > 0) {
                        customTime = countdown;
                        customClock();
                    }
                }
                break;
            //秒杀进行中
            case SNAPINUP:
                if (TextUtils.isEmpty(activeVo.getEnd_time()) || TextUtils.isEmpty(entity.getServer_time())) {
                    return;
                }
                Long endTime = Long.parseLong(activeVo.getEnd_time());
                Long serverTime2 = Long.parseLong(entity.getServer_time());
                Long countdownend = endTime - serverTime2;
                if (countdownend > 86400) {
                    llCountdown.setVisibility(View.GONE);
                    tvKilling.setText(String.format("%S结束", DateUtils.parseTimeStampToDateString(endTime)));
                    tvKilling.setVisibility(View.VISIBLE);
                } else {
                    tvKilling.setVisibility(View.GONE);
                    llCountdown.setVisibility(View.VISIBLE);
                    if (countdownend > 0) {
                        customTime = countdownend;
                        customClock();
                    } else {
                        secondKillZone.setVisibility(View.GONE);
                    }
                }
                break;
            case ENDOFTHE:
                dealState();
                break;
            default:
                break;
        }
    }

    /**
     * @author LK
     * @methods initTheZone
     * @description 描述信息  初始化专区列表
     * @date 2015-1-13 下午1:55:17 参数说明
     */
    private void initTheZone(ActiveListVo data) {
        if (null == theZoneAdapter) {
            theZoneAdapter = new TheZoneAdapter(getActivity(), data.getList());
            gvTheZone.setAdapter(theZoneAdapter);
        }
    }

    /**
     * @author LK
     * @methods initEveryoneIsBuying
     * @description 描述信息 大家都在买
     * @date 2015-1-13 下午2:06:46 参数说明
     */
    private void initEveryoneIsBuying(final ArrayList<ShopWareList> data) {
        if (isAdded()) {
            everyOneIsBuyAdapter = new EveryOneIsBuyAdapter(getActivity());
            gvEveryoneIsBuying.setAdapter(everyOneIsBuyAdapter);
            everyOneIsBuyAdapter.addData(data);
            gvEveryoneIsBuying.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), GoodsDetailActivity.class);
                    intent.putExtra("goodsId", data.get(position).getId());
                    getActivity().startActivity(intent);
                }
            });
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        bannerViewPager.startAutoScroll();
    }

    /**
     * @param arrayList
     * @author lk
     * @createdate 2015-3-23 下午6:08:38
     * @Description: (用一句话描述该方法做什么)
     */
    private void initCategoryViewPager(ArrayList<ImageVo> arrayList) {
        if (isAdded()) {
            if (null == arrayList) {
                return;
            }
            if (null == imagePagerAdapter) {
                imagePagerAdapter = new ImagePagerAdapter(getActivity(), arrayList);
                bannerViewPager.setAdapter(imagePagerAdapter.setInfiniteLoop(true));
                imgList = arrayList;
                InitFocusIndicatorContainer(arrayList.size());
            } else {
                imagePagerAdapter.addData(arrayList);
            }
            InitFocusIndicatorContainer(arrayList.size());
            bannerViewPager.startAutoScroll();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        bannerViewPager.stopAutoScroll();
    }

    /**
     * 设置轮播图上的小圆点
     */
    private void InitFocusIndicatorContainer(int bannerSize) {
        if (!isAdded()) {
            return;
        }
        if (bannerSize == 1) { // 轮播图如果是一张，则不显示小圆点
            llFocusaIndicatorContainer.setVisibility(View.GONE);
        } else {// 循环添加小圆点
            llFocusaIndicatorContainer.removeAllViews();
            for (int i = 0; i < bannerSize; i++) {
                ImageView ivBanner = new ImageView(getActivity());
                ivBanner.setId(i);
                ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
                ivBanner.setScaleType(localScaleType);
                LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                ivBanner.setLayoutParams(localLayoutParams);
                ivBanner.setPadding(5, 5, 5, 5);
                ivBanner.setImageResource(R.drawable.shop_banner_img_selected);
                llFocusaIndicatorContainer.addView(ivBanner);
            }
        }
    }

    /**
     * @param arrayList 类目信息
     * @author czz
     * @createdate 2014-10-15 下午2:50:12
     * @Description: (初始化GridView)
     */
    private void initGridView(ArrayList<ShopWareTypeVo> arrayList) {
        if (null == adapter) {
            adapter = new ActivityShopCityAdapter(getActivity(), arrayList);
            categoryGridView.setSelector(android.R.color.transparent);// 设置GridView的item的点击效果
            categoryGridView.setAdapter(adapter);
        } else {
            adapter.setArrayList(arrayList);
        }
        GoodsListActivity.shopWareTypeVos = arrayList;
        categoryGridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), GoodsListActivity.class);
                intent.putExtra("typePosition", position);
                Constants.SORT_POSITION = 0;
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ivSecondKill || v.getId() == R.id.tv_loadmore) {
            Intent intent = new Intent(getActivity(), SecondKillActivity.class);
            intent.putExtra("id", secondKillId);
            intent.putExtra("activityId", activityId);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        } else if (v.getId() == R.id.et_search_goods || v.getId() == R.id.layout_search) {
            if (adapter != null) {
                Intent intent = new Intent(getActivity(), SearchListActivity.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        } else if (v.getId() == R.id.title_btn_left) {
            getActivity().finish();
        } else if (v.getId() == R.id.reLoadView) {
            initData(true);
        }
    }


    /**
     * @author LK
     * @methods customClock
     * @description 描述信息 倒计时
     * @date 2015-1-25 下午7:53:10
     */
    private void customClock() {
        timing = 0;
        if (null == mHandler && null == runnable) {
            mHandler = new Handler();
            runnable = new Runnable() {
                @Override
                public void run() {
                    long tempTime = customTime;
                    tempTime = tempTime - (timing++);
                    if (tempTime < 0) {
                        timing = 0;
                        dealState();
                    } else {
                        dealTime(tempTime);
                    }
                    long now = SystemClock.uptimeMillis();
                    long next = now + (1000 - now % 1000);
                    mHandler.postAtTime(runnable, next);
                }
            };
            runnable.run();
        }
    }

    /**
     * @author LK
     * @methods dealState
     * @description 处理时间状态
     * @date 2015-2-4 上午11:16:47 参数说明
     */
    private void dealState() {
        switch (activeVo.getStatus()) {
            case NOTATTHE:
                activeVo.setStatus(SNAPINUP);
                entity.setServer_time(activeVo.getStart_time());
                dealActivityState();
                break;
            case SNAPINUP:
                activeVo.setStatus(ENDOFTHE);
                dealActivityState();
                break;
            case ENDOFTHE:
                initData(false);
                break;
            default:
                break;
        }
    }

    private void dealTime(long time) {
        long hours = (time % (24 * 60 * 60)) / (60 * 60);
        long minutes = ((time % (24 * 60 * 60)) % (60 * 60)) / 60;
        long second = ((time % (24 * 60 * 60)) % (60 * 60)) % 60;
        String hoursStr = timeStrFormat(String.valueOf(hours));
        String minutesStr = timeStrFormat(String.valueOf(minutes));
        String secondStr = timeStrFormat(String.valueOf(second));
        tvHour.setText(hoursStr);
        tvMinutes.setText(minutesStr);
        tvSeconds.setText(secondStr);
    }

    private static String timeStrFormat(String timeStr) {
        switch (timeStr.length()) {
            case 1:
                timeStr = "0" + timeStr;
                break;
        }
        return timeStr;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onHeaderRefresh(AbPullToRefreshView view) {
        initData(false);
    }
}
